package com.wenx.activeMqCamel;

import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;
import net.sf.json.JSONArray;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.DefaultMessage;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import static org.apache.activemq.camel.component.ActiveMQComponent.activeMQComponent;

public class activeMQConsumer {
    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext(); // 1. 创建 CamelContext.
        context.addComponent("activemq",activeMQComponent("tcp://10.10.3.186:61616"));

        context.setTracing(true);
        context.start(); // 4. 启动CamelContext.
        context.addRoutes((new activeMQConsumer()).new test1());
        Thread.sleep(Integer.MAX_VALUE);  // 为了保持CamelContext处于工作状态，这里需要sleep主线程
        context.stop(); // 最后停止CamelContext
    }
    public class test1 extends RouteBuilder{
        @Override
        public void configure() throws Exception {
            //from("activemq:topic:abcd")
            from("activemq:queue:Consumer.A.VirtualTopic.jdbc-test2")
                    .process(new Processor() {
                        @Override
                        public void process(Exchange exchange) throws Exception {
                            String body = exchange.getIn().getBody().toString();
                            System.out.println("消费的body="+body);
                           // ReadContext context = JsonPath.parse(body);
//                            List<Map<String, Object>> mapList =
//                                    context.read("$.[?((@.id > 500 && @.name == '王五') || @.id == 123)]");
//                            for(Map<String, Object> map : mapList){
//                                for(Map.Entry<String,Object> eap : map.entrySet()){
//                                    System.out.println("消费的值: "+eap.getKey()+"  "+eap.getValue());
//                                }
//                            }
                            //Object bodys = context.read("$.[?((@.id > 500 && @.name == '王五') || @.id == 123)]");
                            //JSONArray arrayJson = JSONArray.fromObject(mapList);
                           // exchange.getOut().setBody(bodys.toString());
                        }
                    })
                    .setHeader(Exchange.HTTP_METHOD, constant("POST"))
                    .setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
                    .to("log:output");
                    //.to("http://localhost:8080/demo/hello7");
        }
    }
}
